package org.apache.struts.ehr.patient_details.action;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.struts.ehr.medical_records.model.HL7Message;
import org.apache.struts.ehr.medical_records.model.ReplyMessage;
import org.apache.struts.ehr.patient_details.model.PatientDetails;
import ca.uhn.hl7v2.HL7Exception;
import ca.uhn.hl7v2.model.Message;
import ca.uhn.hl7v2.model.v251.message.ADR_A19;
import ca.uhn.hl7v2.parser.EncodingNotSupportedException;
import ca.uhn.hl7v2.parser.PipeParser;

import com.earogya.Constants;
import com.earogya.communicate.Communicator;
import com.earogya.logic.patient_details.ADR_A19_Handler;
import com.earogya.logic.patient_details.CreateADTA04;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

/***
 * 
 * Action for Patient registration
 * 
 * @author Ureka
 *
 */
public class PatientRegistrationFormAction extends ActionSupport{
	private PatientDetails patientDetails;
	private HL7Message message;
	private Date date;
	private List <PatientDetails> patientDetailsList;
	private ReplyMessage replyMessage;


	/**
	 * 
	 * Override action for patient registration form submission
	 * 
	 */
	public String execute() throws Exception {
		Map<String, Object> session=ActionContext.getContext().getSession();
		CreateADTA04 createMessage=new CreateADTA04(patientDetails);
		replyMessage=new ReplyMessage();
		replyMessage.setMessage("empty");
		String msg=createMessage.createMessage(Constants.SENDING_FACILITY);
		message=new HL7Message();
		if(!msg.equals(null)){
			Communicator c=new Communicator();
			String reply=c.sendMessage(msg);
			replyMessage.setMessage("Patient Registered Successfully");
			ADR_A19_Handler handler=new ADR_A19_Handler(createADRA19Msg(reply.toString()));
			patientDetailsList=handler.manageMessage();
		this.patientDetails=patientDetailsList.get(0);
			
		}
				else{
			message.setMessage(Constants.EMPTY);
		}
		return SUCCESS;
	}
	
	public ADR_A19 createADRA19Msg(String message) throws EncodingNotSupportedException, HL7Exception{
		Message hapiMsg  = new PipeParser().parse(message);
		return (ADR_A19)hapiMsg;
	}
	

	/**
	 * 
	 * Validate the user input
	 */
	public void validate(){
		if(patientDetails.getPatient().getGivenName().length()==0){
			addFieldError( "patientDetails.patient.givenName", "Given Name required");
		}
		if(patientDetails.getPatient().getNicNo().length()!=0){
		if(!this.isCorrectNICNo(patientDetails.getPatient().getNicNo())){
			addFieldError( "patientDetails.patient.nicNo", "Incorrect NIC No Format");
		}
		}
		if(patientDetails.getPatientTelephoneList().get(0).getTelephoneNo().length()!=0){
		if(!this.isCorrectTelephoneNumber((patientDetails.getPatientTelephoneList().get(0).getTelephoneNo()))){
			addFieldError( "patientDetails.patientTelephoneList.telephoneNo", "Incorrect Telephone Number Format");
		}
		}
		if(patientDetails.getPatientEmailList().get(0).getEmail().length()!=0){
		if(!this.isCorrectEmailAddress(((patientDetails.getPatientEmailList().get(0).getEmail())))){
			addFieldError( "patientDetails.patient.patientEmail.email", "Incorrect Email Address Format");
		}
		}
	}
	
	/**
	 * Checks whether the given String is in correct NIC format
	 * 
	 * @param nic
	 * @return
	 */
	private Boolean isCorrectNICNo(String nic){
		Boolean isCorrectNicNo=false;
		int length=nic.length();
		if(length==10 && (nic.charAt(9)=='v' || nic.charAt(9)=='V'||nic.charAt(9)=='x'||nic.charAt(9)=='X')){
			isCorrectNicNo=true;
		}
		return isCorrectNicNo;
	}
	
	/**
	 * 
	 * Checks whether the given String in the correct telephone number format
	 * 
	 * @param number
	 * @return
	 */
	private Boolean isCorrectTelephoneNumber(String number){
		Boolean isCorrectNumber=false;
		String[] numbers=number.split(",");
		for(int i=0; i<numbers.length; i++){
			String num=numbers[i].trim();
			if(num.matches("[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]")){
				isCorrectNumber=true;
			}else if(num.matches("[0-9][0-9][0-9]-[0-9][0-9][0-9][0-9][0-9][0-9][0-9]")){
				isCorrectNumber=true;
			}
		}
		return isCorrectNumber;
	}
	
	/**
	 * 
	 * Check whether the given String is in correct email address format
	 * 
	 * @param email
	 * @return
	 */
	private Boolean isCorrectEmailAddress(String email){
		Boolean isCorrectEmail=false;
		if(email.matches("[0-9a-z]+@[a-z]+.[a-z]+")){
			isCorrectEmail=true;
		}
		return isCorrectEmail;
	}

	
	public List<PatientDetails> getPatientDetailsList() {
		return patientDetailsList;
	}

	public void setPatientDetailsList(List<PatientDetails> patientDetailsList) {
		this.patientDetailsList = patientDetailsList;
	}

	public void setMessage(HL7Message message) {
		this.message = message;
	}
	
	public HL7Message getMessage() {
		return message;
	}

	public PatientDetails getPatientDetails() {
		return patientDetails;
	}

	public void setPatientDetails(PatientDetails patientDetails) {
		this.patientDetails = patientDetails;
	}

	public ReplyMessage getReplyMessage() {
		return replyMessage;
	}

	public void setReplyMessage(ReplyMessage replyMessage) {
		this.replyMessage = replyMessage;
	}
}
